package com.runa.rccprotocol.analysis.lichuang;

import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.dto.DtuReport;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.analysis.combination.TouChuanCombinationAnalysis;
import com.runa.rccprotocol.dto.lichuang.LiChuangReportAck;
import org.apache.commons.lang3.StringUtils;

/**
 * LC_FMU
 *
 * @author diandian
 * @Date 2022/4/24 17:47
 */
public class LiChuangFmuDataAnalysis extends ShellBufferAnalysis<byte[]> {

    DtuReport report;

    public LiChuangFmuDataAnalysis(MetaBufferAnalysis<byte[]> analysis) {
        super(analysis, 0);
//        this.report = newComm DtuReport(true);
//        this.report.setMustAck(true);
//        this.report.setAck(newComm LiChuangReportAck());
    }


    @Override
    public int fill(byte[] data, int length,String n) {
        this.interData = null;
        //表示注册报文
        if ((data[0] & 0xff) == 0xAA) {
            return register(data, length);
        } else if (length == 3) {
            if (!StringUtils.isEmpty(netEquNo) && "000000".equals(ByteUtil.byteToHexString(data, 0, 3))) {
                this.interData = report;
            }
            return 0;
        }

        //解析表阀数据
        int fill = this.analysis.fill(data, length,netEquNo);

        this.interData = this.analysis.gain();
        return fill;
    }

    protected int register(byte[] data, int length) {
        int begin = -1;
        for (int i = 0; i < data.length; i++) {
            if ((data[i] & 0xff) == 0xAA) {
                begin = i;
                break;
            }
        }
        if (begin == -1) {
            return 0;
        }
        if (length < (begin + 14)) {
            return -1;
        }
        int surplusLen = length - (begin + 14);

        //判断结束报文
        if ((data[begin + 13] & 0xff) != 0x0D) {
            return surplusLen;
        }
        //判断校验
        byte sum = ByteUtil.sum(data, begin + 1, 11);
        if (sum != (data[begin + 12])) {
            return surplusLen;
        }
        //解析编号
        byte[] serialNo = new byte[10];
        System.arraycopy(data, begin + 2, serialNo, 0, 10);
        this.netEquNo = new String(serialNo);
        this.report = new DtuReport(netEquNo);
        this.report.setMustAck(true);
        this.report.setAck(new LiChuangReportAck());
        this.interData = report;
        return surplusLen;
    }


    @Override
    public IInterData gain() {
        return this.interData;
    }

    public static void main(String[] args) {
//        byte[] data = ByteUtil.hexStringToByte("AA 01 38 31 31 31 30 31 35 31 36 36 FF 0D".replace(" ", ""));
        byte[] data = ByteUtil.hexStringToByte("FEFE682056176030005942812E901F0005067481020506748102170000000035000000002C639946008625000926005610013816192107222008FFFEFE682000176030005942812E901F0005206646020520664602170000000035000000002C636938002326003326008600012210192107222000ADD516FEFE682034176030005942812E901F0005335246050533524605170000000035000000002C500220017625008625000747011514192107222000BDEF16FEFE682029146030005942812E901F0005691706000569170600170000000035000000002C918701001026003826008602004331192107222008BDCD16FEFE682089106030005942812E901F0005975054020597505402170000000035000000002C914719005925008125000746013209192107222008AD7C16FEFE682072176030005942812E901F0005497686030549768603170000000035000000002C840568007225004225007906013613192107222000AD9416FEFE682069116030005942812E901F0005757129020575712902170000000035000000002C363716004625005725002732015816192107222000AD9116FEFE682030126030005942812E901F0005024527040502452704170000000035000000002C426297006726005226002443015722192107222000BD1A16FEFE682051106030005942812E901F0005434281030543428103170000000035000000002C349798007825000026002945012611192107222008BD1A16FEFE682005106030005942812E901F0005221520040522152004170000000035000000002C507587008025001426009446013324192107222008BD0316FEFE682084186030005942812E901F0005752364050575236405170000000035000000002C190139017225008425002644014309192107222000BDBC16FEFE682079106030005942812E901F0005172695020517269502170000000035000000002C489961007525003725008942013717192107222000BD5616FEFE682082106030005942812E901F0005591682020559168202170000000035000000002C085956006125007325004210014824192107222000BDDF16FEFE682002106030005942812E901F0005496091030549609103170000000035000000002C210779009425009525009807012500192107222000AD2816FEFE682070106030005942812E901F0005530403050553040305170000000035000000002C212877007825007425009243011517192107222000BD0916FEFE682039066030005942812E901F0005236088000523608800170000000035000000002C256207008125008525004038004809192107222000BDCF16FEFE682000116030005942812E901F0005459158030545915803170000000035000000002C992674006425007925003046014112192107222000BD6A16FEFE682056106030005942812E901F0005806623040580662304170000000035000000002C961787006125006625006342015110192107222000BD9F16FEFE682002086030005942812E901F0005903004020590300402170000000035000000002C333626000126006725006469000501192107222000AD6E16FEFE682037096030005942812E901F0005175918020517591802170000000035000000002C266258008125008725005872002518192107222000BD6016FEFE682096106030005942812E901F0005000000000500000000170000000035000000002C2000000050250047240000000051061921072220028DA216FEFE682032136630005942812E901F0005606011060560601106170000000035000000002C557911016325006425004010010415192107222000BD2716000000".replace(" ", ""));
        TouChuanCombinationAnalysis touChuanCombinationAnalysis = new TouChuanCombinationAnalysis();
        LiChuangFmuDataAnalysis analysis = new LiChuangFmuDataAnalysis(touChuanCombinationAnalysis);
        analysis.fill(data, data.length,"");
        byte[] serialNo = new byte[10];

        System.arraycopy(data, 0 + 2, serialNo, 0, 10);

        System.out.println(new String(serialNo));
    }

}